package internationalenergytradingplatform.controller.BuyerController;

import internationalenergytradingplatform.context.BaseContext;
import internationalenergytradingplatform.dao.BuyerAccountDao;
import internationalenergytradingplatform.pojo.BuyerAccount;
import internationalenergytradingplatform.result.Result;
import internationalenergytradingplatform.utils.PayUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("alipay")
public class ALiPayController {

    @Autowired
    BuyerAccountDao buyerAccountDao;

    @GetMapping("/pay")
    public Result index(HttpServletResponse response, HttpServletRequest request, double money) {
        String paymentUrl = new PayUtil().toImGES(response, UUID.randomUUID().toString().replaceAll("-", "")
                , "充值", money, "192.168.0.01");
        log.info("==>paymentUrl：{}", paymentUrl);
        //系统将用户充值的钱写进数据库中
        String buyer = BaseContext.getCurrentId();
        BuyerAccount buyerByTele = buyerAccountDao.getBuyerByTele(buyer);
        buyerAccountDao.updateBalance(buyerByTele.getBalance() + money*100, buyer);
        return Result.success(paymentUrl);
    }
}
